<?php 
/**
*file art.php
*单个文章页面显示&&评论显示
* 
*/
define('ACC', true);
require('./include/init.php');

$cat = new CatModel();
//栏目列表及栏目下文章数显示
$catnamenum = $cat->catnameNum();
//文章art
$art = new ArtModel();
//最新发布的5条文章显示
$new = $art->artNew();
//最新5条历史记录显示
$uri = $_COOKIE['history'];
$his = History($uri);
$hlist = $art->getHistory($his);
//print_r($hlist);

$art_id = isset($_GET['art_id'])?$_GET['art_id']+0:header('location:index.php');
//根据art_id查询一篇文章
$art = new ArtModel();
$one = $art->artOne($art_id);
//查询art_id下所有评论
$comment = new commentModel();
$commart = $comment->commentArt($art_id);

//判断是否写评论
if (empty($_POST)) {
	include(ROOT.'view/front/artcomm.html');
}else {
	$data = $comment->_fields($_POST); //过滤
	$data = $comment->_autoFill($data);//填充
//登录用户 调用登录用户信息 跟session验证
 if(isset($_SESSION['username'])){
    $login = new LoginModel();
    $user = $login->getUserRow($_SESSION['username']);
    
	$data['username'] = $_SESSION['username'];
	$data['user_id'] = $user['user_id'];
	$data['email'] = $user['email'];
	$data['nick'] = $user['nick'];
	$data['comment'] = $_POST['comment'];
}else {
	//匿名用户数据处理
	$data = $comment->_fields($_POST); //过滤
	$data = $comment->_autoFill($data);//填充
	$valids = $comment->_valiDat($data); //验证
    if (!$valids) {
	$info = implode($comment->getError());
	include(ROOT.'view/front/info.html');
	exit;
    }
}	
//匿名评论 写入数据库
if(!$comment->add($data)){
	$info = '评论失败';
	include(ROOT.'view/front/info.html');
	exit;
}else {
	//到这一步 art表有个评论数字段comm 评论成功art_id对应comm+1

    $comment_id = $comment->insert_id(); //获取刚添加的comment_id
    //art comm+1
    if (!$art->commPlus($art_id)){
    	if($comment->delete($comment_id)){
    		$info = '评论失败 删除评论';
	        include(ROOT.'view/front/info.html');
	        exit;
    	  }
    }else {
    //评论成功跳转当前页
     header("location:$_SERVER[REQUEST_URI]");
    }
}







}
?>